package com.object.module.cbb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.configuration.utils.PageUtils;
import com.object.configuration.utils.Q;
import com.object.configuration.utils.Query;
import com.object.dao.cbb.CddShopDao;
import com.object.module.cbb.entity.CbbPhotoAlbum;
import com.object.module.cbb.entity.CddShopEntity;
import com.object.module.cbb.service.CbbPhotoAlbumService;
import com.object.module.cbb.service.CbbSortService;
import com.object.module.cbb.service.CddShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("cddShopService")
public class CddShopServiceImpl extends ServiceImpl<CddShopDao, CddShopEntity> implements CddShopService {

    @Autowired
    private CbbPhotoAlbumService photoAlbumService;

    @Autowired
    private CbbSortService cbbSortService;


    /**
     * 分页查询商品信息
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CddShopEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String spName = (String) params.get("spName");
        String xlAscending = (String) params.get("xlAscending");
        String priceAscending = (String) params.get("priceAscending");
        String startPrice = (String) params.get("startPrice");
        String endPrice = (String) params.get("endPrice");
        if (!StringUtils.isEmpty(spName)) {
            wrapper.like("sp_name", spName);
        }
        if (StringUtils.isNotEmpty(xlAscending)) {
            boolean xlacs = Boolean.parseBoolean(xlAscending);
            if (xlacs) {
                wrapper.orderByDesc("sp_sales");
            } else {
                wrapper.orderByAsc("sp_sales");
            }
        }
        if (StringUtils.isNotEmpty(priceAscending)) {
            boolean pricasc = Boolean.parseBoolean(priceAscending);
            if (pricasc) {
                wrapper.orderByDesc("sp_selling_price");
            } else {
                wrapper.orderByAsc("sp_selling_price");
            }
        }

        //  startPrice<=sp_selling_price<=sp_selling_price
        if (StringUtils.isNotEmpty(startPrice)) {
            wrapper.ge("sp_selling_price", startPrice);
        }
        if (StringUtils.isNotEmpty(endPrice)) {
            wrapper.le("sp_selling_price", endPrice);
        }

        if (StringUtils.isNotEmpty(key)) {
            wrapper.eq("sp_name", key);
        }

        IPage<CddShopEntity> page = this.page(
                new Query<CddShopEntity>().getPage(params),
                wrapper
        );
        for (CddShopEntity record : page.getRecords()) {
            record.setSort(cbbSortService.getById(record.getStId()));
        }
        return new PageUtils(page);
    }

    /**
     * 修改销量
     *
     * @param spId
     * @param seckillSales
     */
    public void updateSales(Long spId, Integer seckillSales) {
        CddShopEntity shopEntity = new CddShopEntity();
        shopEntity.setSpId(spId);
        shopEntity.setSpSales(shopEntity.getSpSales() + seckillSales);
        updateById(shopEntity);
    }

    /**
     * 热门推荐
     *
     * @return
     */
    @Override
    public Q popularRecommendation() {
        QueryWrapper<CddShopEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sp_rm", 1);
        List<CddShopEntity> list = list(wrapper);
        return Q.ok().put("shops", list);
    }

    //最新上架
    @Override
    public Q newArrival() {
        QueryWrapper<CddShopEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sp_create_time");
        List<CddShopEntity> list = list(wrapper);
        return Q.ok().put("shops", list);
    }

    //人气商品
    @Override
    public Q popularProduct() {
        QueryWrapper<CddShopEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sp_sales");
        List<CddShopEntity> list = list(wrapper);
        return Q.ok().put("shops", list);
    }


    @Value("${img..witdh}")
    private String imgWitdh;

    @Override
    public CddShopEntity getByIdInfo(Integer spId) {
        CddShopEntity shopEntity = getById(spId);
        QueryWrapper<CbbPhotoAlbum> wrapper = new QueryWrapper<>();
        wrapper.eq("sp_id", spId);
        shopEntity.setPhotoAlbums(photoAlbumService.list(wrapper));
        shopEntity.setSpText(shopEntity.getSpText() + "<style>img{ width:" + imgWitdh + ";}</style>");
        return shopEntity;
    }

    @Override
    public void saveShop(CddShopEntity cddShop) {
        cddShop.setSpCreateTime(new Date());
        save(cddShop);
    }


}